package com.lwc.cfdns.filter;

import com.lwc.cfdns.common.RequestWrapper;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * 基础过滤器
 *
 * @author luwc
 * @title BaseFilter
 * @description
 * @time 2023/5/21 8:03
 * @version: 1.0
 */
@WebFilter(urlPatterns = "/*", filterName = "BaseFilter")
public class BaseFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        ServletRequest requestWrapper = null;
        if (servletRequest instanceof HttpServletRequest) {
            // 解决HttpServletRequest不可重复使用InputStream,报错报Stream closed的问题
            requestWrapper = new RequestWrapper((HttpServletRequest) servletRequest);
        }
        if (requestWrapper == null) {
            filterChain.doFilter(servletRequest, servletResponse);
        } else {
            filterChain.doFilter(requestWrapper, servletResponse);
        }
    }

    @Override
    public void destroy() {

    }
}
